package com.bs.sc.mgr.order.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.bs.sc.base.utils.AjaxJson;
import com.bs.sc.base.utils.StringUtil;
import com.bs.sc.front.goodsCar.dao.GoodsCarDao;
import com.bs.sc.front.goodsCar.dao.GoodsCarItemDao;
import com.bs.sc.front.user.entity.User;
import com.bs.sc.mgr.goods.dao.GoodsDao;
import com.bs.sc.mgr.goods.entity.Goods;
import com.bs.sc.mgr.order.dao.OrderDao;
import com.bs.sc.mgr.order.dao.OrderItemDao;
import com.bs.sc.mgr.order.entity.Order;
import com.bs.sc.mgr.order.entity.OrderItem;
import com.bs.sc.mgr.order.service.OrderService;

@Service("orderService")
@Transactional
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private GoodsCarDao goodsCarDao;
	@Autowired
	private GoodsCarItemDao goodsCarItemDao;
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private OrderItemDao orderItemDao;

	@Override
	public Page<Order> findOrderList(Pageable pageable) {
		return orderDao.findAll(pageable);
	}

	@Override
	public AjaxJson getOrder(String id) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取订单信息失败");
		Optional<Order> optional = orderDao.findById(id);
		if (optional.isPresent()) {
			Order order = optional.get();
			List<OrderItem> orderItems = orderItemDao.findByOrder(order);
			j.setCode("success");
			j.setMsg("获取订单信息成功");
			j.setData(orderItems);
		}
		return j;
	}

	@Override
	public AjaxJson createOrder(HttpServletRequest request, Order order, String[] goodsIds) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("创建订单失败");
		try {
			// 1、获取用户session
			User user = (User) request.getSession().getAttribute("user");
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			// 2、创建订单
			order.setUser(user);
			order.setOrderId(StringUtil.generateOrderId());
			order.setStatus(0);
			orderDao.save(order);
			List<Goods> goodsList = goodsDao.findGoodsByGoodsIds(goodsIds);
			for (Goods goods : goodsList) {
				OrderItem item = new OrderItem();
				item.setGoods(goods);
				item.setOrder(order);
				orderItemDao.save(item);
			}
			// 3、删除用户购物车数据
			List<Map<String, Object>> cars = goodsCarDao.findOrderGoods(user.getId(), goodsIds);
			String[] carIds = new String[cars.size()];
			for (int i = 0; i < cars.size(); i++) {
				carIds[i] = (String) cars.get(i).get("carId");
				// 删除购物车详细条目
				goodsCarItemDao.deleteByGoodsCarId(carIds);
				goodsCarDao.deleteById((String) cars.get(i).get("carId"));
			}
			j.setCode("success");
			j.setMsg("创建订单成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public AjaxJson getMyOrder(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取我的订单失败");
		// 1、获取用户session
		User user = (User) request.getSession().getAttribute("user");
		if (user == null) {
			j.setMsg("请登录");
			return j;
		}
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		List<Order> orders = orderDao.findByUser(user);
		for (Order order : orders) {
			Map<String, Object> map = new HashMap<String, Object>();
			List<OrderItem> items = orderItemDao.findByOrder(order);
			map.put("orderId", order.getOrderId());
			map.put("goodsList", items);
			list.add(map);
		}
		j.setCode("success");
		j.setMsg("获取我的订单成功");
		j.setData(list);
		return j;
	}

	@Override
	public AjaxJson updateItemGrade(String itemId, int grade, String comment) {
		System.out.println("明细id：" + itemId + "，评分：" + grade);
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("评分失败");
		Optional<OrderItem> optional = orderItemDao.findById(itemId);
		if (optional.isPresent()) {
			OrderItem item = optional.get();
			item.setGrade(grade);
			item.setComment(comment);
			orderItemDao.save(item);
			j.setCode("success");
			j.setMsg("评分成功");
		}
		return j;
	}

	@Override
	public AjaxJson deleteItemGrade(String itemId) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("删除商品失败");
		Optional<OrderItem> optional = orderItemDao.findById(itemId);
		if (optional.isPresent()) {
			OrderItem item = optional.get();
			orderItemDao.delete(item);
			j.setCode("success");
			j.setMsg("删除商品成功");
		}
		return j;
	}
}
